package com.tdzx.view;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

/**
 * 自定义字母选择控件
 *  注意接口问题
 * @author majie
 *
 */
public class CustomLetterView extends View {

//	private String[] letter = {"A","B","C","D","E","F","G","H","I","J","K",
//			"L", "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	private String[] letter = { "","A", "B", "C", "D", "F", "G", "H", "J",
			"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y",
			"Z" };
	private List<String> groupList;
	private DisplayMetrics displayMetrics;
	private OnTouchingLetterChangedListener onTouchingLetterChangedListener; 
//	int selected = 1;

	public CustomLetterView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (groupList == null) {
			return;
		}
		
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextAlign(Align.CENTER);
		paint.setTextSize(14);
		paint.setTypeface(Typeface.DEFAULT_BOLD);

		int length = letter.length;
		float density = displayMetrics.density;
//		int width = this.getWidth();
		int everyHeight = this.getHeight() / length;
//		Log.i("snow", "width=" + width + "    getHeight()=" + this.getHeight());
		for (int i = 0; i < length; i++) {
			canvas.drawText(letter[i], 13 * density, everyHeight * (i + 1), paint);
			
			/*if(i == selected) {
				paint.setColor(Color.CYAN);
				canvas.drawRect(13 * density - 20, everyHeight * (i + 1) - 30, 13 * density + 10 * density, everyHeight * (i + 1) + 10 * density, paint);
				paint.setColor(Color.BLACK);
				canvas.drawText(letter[i], 13 * density, everyHeight * (i + 1), paint);
			}*/
		}
	}

	public void setList(List<String> groupList, DisplayMetrics displayMetrics) {
		this.groupList = groupList;
		this.displayMetrics = displayMetrics;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (y / getHeight() * letter.length);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_MOVE:
			if (listener != null && groupList != null && c > 0 && c < letter.length) {
//				if (groupList.contains(letter[c])) {
//					listener.onTouchingLetterChanged(c);
////					invalidate();
//				}
				int size = groupList.size();
				for (int i = 0; i < size; i++) {
					if (groupList.get(i).equals(letter[c])) {	//比较字母和组的列表看点击的和哪一个匹配，就知道跳转到哪一个组了
						listener.onTouchingLetterChanged(i);
						
//						selected = c;
						
						break;
					}
				}
			}
			break;
		case MotionEvent.ACTION_UP:
//			invalidate();
			break;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	/**
	 *  创建一个接口
	 * @author majie
	 *
	 */
	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(int groupPosition);
	}

}